package com.hainatong.jcmvvmbase.repository;

import androidx.lifecycle.MutableLiveData;
import com.hainatong.jcmvvmbase.api.ApiResult;
import com.hainatong.jcmvvmbase.api.ApiService;
import com.hainatong.jcmvvmbase.api.BaseObserver;
import com.hainatong.jcmvvmbase.login.entity.LoginResult;
import com.hainatong.jcmvvmbase.login.entity.UserBean;
import com.hainatong.jcmvvmbase.login.entity.UserResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import java.util.HashMap;
import java.util.Map;

public class UserRepository extends BaseRepository
{
    private UserRepository()
    {}

    private static class SingletonInstance
    {
        private static final UserRepository INSTANCE = new UserRepository();
    }

    public static UserRepository getInstance()
    {
        return SingletonInstance.INSTANCE;
    }


    private MutableLiveData<LoginResult> loginResult;

    public MutableLiveData<LoginResult> getLoginResult()
    {
        return loginResult;
    }

    private MutableLiveData<Boolean> updateResult;

    public MutableLiveData<Boolean> getUpdateResult()
    {
        return updateResult;
    }

    public MutableLiveData<LoginResult> login(String userName , String pwd)
    {
        if( loginResult  == null )
        {
            loginResult = new MutableLiveData<>();
        }
        Map<String, String> params = new HashMap<>();
        params.put("username", userName);
        params.put("password", pwd);

        ApiService.Builder.getSiteHelperServer().login(params)
                .compose(RxSchedulersUtils.Obs_io_main())
                .subscribe(new BaseObserver<ApiResult<UserResult>>( this )
                {
                    @Override
                    public void onError(int code, String errorMsg)
                    {
                        getLoginResult().postValue( new LoginResult(false , errorMsg) );
                    }

                    @Override
                    public void onSuccess(ApiResult<UserResult> result)
                    {
                        if ( result.getCode() == 0 )
                        {
                            getLoginResult().postValue( new LoginResult(true , result.getResult().getUser()) );
                        }
                        else
                        {
                            getLoginResult().postValue( new LoginResult(false,result.getMessage()) );
                        }
                    }
                });

        return loginResult;
    }


    public MutableLiveData<Boolean> updatePwd(String newPwd )
    {
        if( updateResult  == null )
        {
            updateResult = new MutableLiveData<>();
        }
        Map<String, String> params = new HashMap<>();
        params.put("username", "user");
        params.put("password", "123");
        ApiService.Builder.getSiteHelperServer().login(params)
                          .compose(RxSchedulersUtils.Obs_io_main())
                          .subscribe(new BaseObserver<ApiResult<UserResult>>(this)
                          {
                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getUpdateResult().postValue( false );
                              }

                              @Override
                              public void onSuccess(ApiResult<UserResult> result)
                              {
                                  getUpdateResult().postValue(true);
                              }
                          });
        return  updateResult;
    }


}
